package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.activity.ActivityDTO;
import com.qk.entity.Activity;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: hm
 * @Date: 2025/10/09 15:01
 * @Description:
 */
@Service
@SuppressWarnings("all")
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public List<Activity> getByType(Integer type) {
        return activityMapper.selectByType(type);
    }

    @Override
    public void update(Activity activity) {
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }

    @Override
    public Activity getById(Integer id) {
        return activityMapper.selectById(id);
    }

    @Override
    public void deleteById(Integer id) {
        activityMapper.deleteById(id);
    }

    @Override
    public PageResult<Activity> list(ActivityDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Activity> page = activityMapper.selectByPage(dto);
        Page<Activity> result = (Page<Activity>) page;
        return PageResult.<Activity>builder()
                .total(result.getTotal())
                .rows(result.getResult())
                .build();
    }

    @Override
    public void createActivity(Activity activity) {
        boolean hasNull = BeanUtil.hasNullField(activity, "id", "discount", "voucher", "createTime", "updateTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.insert(activity);
    }
}
